package com.zcz.item.controller;

import com.zcz.item.pojo.Category;
import com.zcz.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 王卫帅
 */
@Controller
@RequestMapping("category/")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("list")
    public ResponseEntity<List<Category>> query(@RequestParam(value = "pid",defaultValue = "0")Long pid){
       //参数是空
        if (pid == null || pid <0){
            return ResponseEntity.badRequest().build();
        }
        List<Category> categories = this.categoryService.queryCaegoriesByPid(pid);
        //没有信息
        if (CollectionUtils.isEmpty(categories)){
            return ResponseEntity.notFound().build();
        }
        //200
        return ResponseEntity.ok(categories);
    }
}
